1   /*
2    * Copyright (C) 2008 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.testers;
18  
19  import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
20  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
21  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
22  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
23  import static com.google.common.collect.testing.features.MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
24  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
25  
26  import com.google.common.annotations.GwtCompatible;
27  import com.google.common.collect.testing.AbstractMapTester;
28  import com.google.common.collect.testing.WrongType;
29  import com.google.common.collect.testing.features.CollectionSize;
30  import com.google.common.collect.testing.features.MapFeature;
31  
32  import java.util.ConcurrentModificationException;
33  import java.util.Iterator;
34  import java.util.Map.Entry;
35  
36  /**
37   * A generic JUnit test which tests {@code remove} operations on a map. Can't be
38   * invoked directly; please see
39   * {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
40   *
41   * @author George van den Driessche
42   * @author Chris Povirk
43   */
44  @SuppressWarnings("unchecked") // too many "unchecked generic array creations"
45  @GwtCompatible
46  public class MapRemoveTester<K, V> extends AbstractMapTester<K, V> {
47    @MapFeature.Require(SUPPORTS_REMOVE)
48    @CollectionSize.Require(absent = ZERO)
49    public void testRemove_present() {
50      int initialSize = getMap().size();
51      assertEquals("remove(present) should return the associated value",
52          samples.e0.getValue(), getMap().remove(samples.e0.getKey()));
53      assertEquals("remove(present) should decrease a map's size by one.",
54          initialSize - 1, getMap().size());
55      expectMissing(samples.e0);
56    }
57  
58    @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION,
59        SUPPORTS_REMOVE})
60    @CollectionSize.Require(SEVERAL)
61    public void testRemovePresentConcurrentWithEntrySetIteration() {
62      try {
63        Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator();
64        getMap().remove(samples.e0.getKey());
65        iterator.next();
66        fail("Expected ConcurrentModificationException");
67      } catch (ConcurrentModificationException expected) {
68        // success
69      }
70    }
71  
72    @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION,
73        SUPPORTS_REMOVE})
74    @CollectionSize.Require(SEVERAL)
75    public void testRemovePresentConcurrentWithKeySetIteration() {
76      try {
77        Iterator<K> iterator = getMap().keySet().iterator();
78        getMap().remove(samples.e0.getKey());
79        iterator.next();
80        fail("Expected ConcurrentModificationException");
81      } catch (ConcurrentModificationException expected) {
82        // success
83      }
84    }
85  
86    @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION,
87        SUPPORTS_REMOVE})
88    @CollectionSize.Require(SEVERAL)
89    public void testRemovePresentConcurrentWithValuesIteration() {
90      try {
91        Iterator<V> iterator = getMap().values().iterator();
92        getMap().remove(samples.e0.getKey());
93        iterator.next();
94        fail("Expected ConcurrentModificationException");
95      } catch (ConcurrentModificationException expected) {
96        // success
97      }
98    }
99  
100   @MapFeature.Require(SUPPORTS_REMOVE)
101   public void testRemove_notPresent() {
102     assertNull("remove(notPresent) should return null",
103         getMap().remove(samples.e3.getKey()));
104     expectUnchanged();
105   }
106 
107   @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_KEYS})
108   @CollectionSize.Require(absent = ZERO)
109   public void testRemove_nullPresent() {
110     initMapWithNullKey();
111 
112     int initialSize = getMap().size();
113     assertEquals("remove(null) should return the associated value",
114         getValueForNullKey(), getMap().remove(null));
115     assertEquals("remove(present) should decrease a map's size by one.",
116         initialSize - 1, getMap().size());
117     expectMissing(entry(null, getValueForNullKey()));
118   }
119 
120   @MapFeature.Require(absent = SUPPORTS_REMOVE)
121   @CollectionSize.Require(absent = ZERO)
122   public void testRemove_unsupported() {
123     try {
124       getMap().remove(samples.e0.getKey());
125       fail("remove(present) should throw UnsupportedOperationException");
126     } catch (UnsupportedOperationException expected) {
127     }
128     expectUnchanged();
129     assertEquals("remove(present) should not remove the element",
130         samples.e0.getValue(), get(samples.e0.getKey()));
131   }
132 
133   @MapFeature.Require(absent = SUPPORTS_REMOVE)
134   public void testRemove_unsupportedNotPresent() {
135     try {
136       assertNull("remove(notPresent) should return null or throw "
137           + "UnsupportedOperationException",
138           getMap().remove(samples.e3.getKey()));
139     } catch (UnsupportedOperationException tolerated) {
140     }
141     expectUnchanged();
142     expectMissing(samples.e3);
143   }
144 
145   @MapFeature.Require(
146       value = SUPPORTS_REMOVE,
147       absent = ALLOWS_NULL_KEY_QUERIES)
148   public void testRemove_nullQueriesNotSupported() {
149     try {
150       assertNull("remove(null) should return null or throw "
151           + "NullPointerException",
152           getMap().remove(null));
153     } catch (NullPointerException tolerated) {
154     }
155     expectUnchanged();
156   }
157 
158   @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_KEY_QUERIES})
159   public void testRemove_nullSupportedMissing() {
160     assertNull("remove(null) should return null", getMap().remove(null));
161     expectUnchanged();
162   }
163 
164   @MapFeature.Require(SUPPORTS_REMOVE)
165   public void testRemove_wrongType() {
166     try {
167       assertNull(getMap().remove(WrongType.VALUE));
168     } catch (ClassCastException tolerated) {
169     }
170     expectUnchanged();
171   }
172 }